﻿using System;
using System.IO;
using System.Threading.Tasks;

namespace Ewell.Interface.Common.Utils
{
    public sealed class LogHelper
    {
        private static string logFilePath;
        private static readonly object lockObject = new object();
        //private static ILog _logger;
        //private static readonly Dictionary<string, ILog> _loggerInstances = new Dictionary<string, ILog>();
        private LogHelper() { }
        public static void Init(string folderName, string logName)
        {
            #region Log4Net作废
            //var logger = GetLogger(folderName, logName);
            //if (logger != null)
            //{
            //    _logger = logger;
            //    return;
            //}
            //var hierarchy = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
            //var appender = new RollingFileAppender();
            //var layout = new PatternLayout("[%date] %level %logger %ndc - %message%newline");

            //layout.ActivateOptions();

            //appender.AppendToFile = true;
            //appender.File = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", folderName, $"{logName}.log");
            //appender.Layout = layout;
            //appender.MaxSizeRollBackups = 10;
            //appender.RollingStyle = RollingFileAppender.RollingMode.Size;
            //appender.MaximumFileSize = "10MB";
            //appender.StaticLogFileName = true;

            //// 设置文件不独占
            //appender.LockingModel = new FileAppender.MinimalLock();

            //appender.ActivateOptions();

            //hierarchy.Root.Level = Level.All;
            //hierarchy.Configured = true;

            //var key = $"{folderName}_{logName}";
            //if (!_loggerInstances.ContainsKey(key))
            //{
            //    lock (_loggerInstances)
            //    {
            //        if (!_loggerInstances.ContainsKey(key))
            //        {
            //            hierarchy.Root.AddAppender(appender);
            //            _logger = LogManager.GetLogger("InterfaceLog");
            //            _loggerInstances.Add(key, _logger);
            //        }
            //    }
            //}
            #endregion

            LogHelper.logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", folderName, $"{logName}.log");
            string directoryPath = Path.GetDirectoryName(LogHelper.logFilePath);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            if (!File.Exists(LogHelper.logFilePath))
            {
                File.Create(LogHelper.logFilePath).Close();
            }
        }

        //public static ILog GetLogger(string folderName, string logName)
        //{
        //    var key = $"{folderName}_{logName}";
        //    if (!_loggerInstances.ContainsKey(key))
        //    {
        //        lock (_loggerInstances)
        //        {
        //            if (!_loggerInstances.ContainsKey(key))
        //            {
        //                return null;
        //            }
        //        }
        //    }

        //    return _loggerInstances[key];
        //}

        public static async void Debug(string message)
        {
            Console.WriteLine(message);
            //await Task.Run(() =>
            //{
            //    lock (_loggerInstances)
            //    {
            //        if (_logger != null && _logger.IsInfoEnabled)
            //        {
            //            _logger.Info(message);
            //        }
            //    }
            //});
            await Task.Run(() =>
            {
                lock (lockObject)
                {
                    if (logFilePath == null) return;
                    using (StreamWriter writer = File.AppendText(logFilePath))
                    {
                        writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}-Debug: {message}");
                    }
                }
            });
        }

        public static async Task Info(string message)
        {
            //_logger.Info(message);
            await Task.Run(() =>
            {
                lock (lockObject)
                {
                    using (StreamWriter writer = File.AppendText(logFilePath))
                    {
                        writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}-Info: {message}");
                    }
                }
            });
        }

        public static async void Warn(string message)
        {
            //_logger.Warn(message);
            await Task.Run(() =>
            {
                lock (lockObject)
                {
                    using (StreamWriter writer = File.AppendText(logFilePath))
                    {
                        writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}-Warn: {message}");
                    }
                }
            });
        }

        public static async void Error(string message)
        {
            //_logger.Error(message);
            await Task.Run(() =>
            {
                lock (lockObject)
                {
                    using (StreamWriter writer = File.AppendText(logFilePath))
                    {
                        writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}-Error: {message}");
                    }
                }
            });
        }

        public static async void Fatal(string message)
        {
            //_logger.Fatal(message);
            await Task.Run(() =>
            {
                lock (lockObject)
                {
                    using (StreamWriter writer = File.AppendText(logFilePath))
                    {
                        writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}-Fatal: {message}");
                    }
                }
            });
        }
    }
}
